package com.Socket;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Logger;

public class HelloClient {
    private static final Logger logger = Logger.getLogger(HelloClient.class.getName());

    public Object send(Message message, String host, int port){
        // 1. 创建Socket并连接指定服务器的地址和端口
        try (Socket socket = new Socket(host, port)){
            // 2.通过输出流向服务器发送请求信息
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            objectOutputStream.writeObject(message);
            //3.通过输入流接收服务器返回的响应信息
            ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
            return objectInputStream.readObject();
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        HelloClient client = new HelloClient();
        Message message = (Message) client.send(new Message("Hello,Server"), "127.0.0.1", 6666);
        logger.info("接收到服务器信息：" + message);
    }
}
